package com.yuebao.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.yuebao.config.AlipayProperties;
import com.yuebao.converter.TransToAlipayDTO;
import com.yuebao.exception.CustomerException;
import com.yuebao.model.pojo.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class AlipayClientService {
    
    @Resource
    DefaultAlipayClient alipayClient;
    
    @Resource
    AlipayProperties alipayProperties;
    
    public String sentToPayAndGetForm(Transaction transaction) throws CustomerException {
        
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setBizContent(TransToAlipayDTO.transToBizContent(transaction));
        request.setNotifyUrl(alipayProperties.getNotifyUrl());
        
        String form = "";
        try {
           form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
            log.info("支付宝成功返回页面信息");
        } catch (AlipayApiException e) {
            log.error("支付宝ERROR{}",e.getMessage());
            throw new CustomerException(e.getMessage());
        }
        return form;
    }
    
}
